package com.test;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import com.acrm.client.entity.staff.Staff;
import com.acrm.client.exception.InteractionException;
import com.acrm.client.exception.ServerThrowException;
import com.caucho.hessian.client.HessianHttpProxyFactory;
import com.caucho.hessian.client.HessianProxyFactory;
import com.caucho.hessian.io.CalendarHandle;
import com.caucho.hessian.io.GxdInputStream;
import com.pccw.acrm.entity.UserInfo;
import com.pccw.hessian.support.cache.CacheHandler;
import com.pccw.hessian.support.cache.DefaultCacheHandler;
import com.pccw.hessian.support.client.HessianCAHProxyFactory;
import com.pccw.hessian.support.client.TaskExecuteListener;
import com.pccw.service.acrm.AcrmApi;
import com.pccw.service.acrm.PublicService;
/**
 * @author {guxuede}
 */
public class Mian {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		long a=Runtime.getRuntime().totalMemory();
		System.out.println(a);
		execute2();
	}
	
	
	public static void execute2(){
        //String url = "http://127.0.0.1:8080/HessianServer/remoting/acrmapi";
		//parseAnnotations(PublicService.class);
		  String url = "http://192.168.1.45:8080/CRMServer/remoting/acrmapi";
        HessianCAHProxyFactory proxyFactory = new HessianCAHProxyFactory();
        proxyFactory.setConnectTimeout(3000);
        proxyFactory.setReadTimeout(3000); 
        proxyFactory.setLocalEnabled(true);
        proxyFactory.setHessian2Reply(false);
       // proxyFactory.setDebug(true);
        HessianCAHProxyFactory.setCookieSeterName("Set-Cookie");
        try {
        	 PublicService api=proxyFactory.create(PublicService.class, url,
        			 new DefaultCacheHandler(),
        			 new CRMAsynTaskExecuter<Object, Throwable>(),
        			 new CRMExceptionHandler(),null);
        	// parseAnnotations(api.getClass());
        	//Thread.sleep(1000);
        	//System.out.println(api.login("guxuede", "123456"));
        //	System.out.println(GxdInputStream.getAccumulatTotalLength());
        //	System.out.println(api.getAllAccountList());
        	
//        	api.login("guxuede","123456",new CRMDefaultTaskListener<Staff,Throwable>(){
//        		@Override
//        		public void onExecuteSuccess(Method method, Object[] args,Staff result) {
//        			System.out.println("login sucess："+result);
//        		}
//        		@Override
//        		public void onExceptionOccured(Method method, Object[] args,Throwable exception) {
//        			//exception.printStackTrace();
//        			System.out.println("异常："+exception);
//        		}
//        	});
        	 
       	 System.out.println(api.login("guxuede","123456"));
       	System.out.println(api.getLoginUser());
//        	 System.out.println(api.login("guxuede","123456"));
//        	Thread.sleep(15000);
//        	System.out.println(api.login("guxuede","123456"));
		} catch (Exception e) {
			//System.out.println(e.getCause());
			e.printStackTrace();
			//CRMUtils.getCausedUntil(e, InteractionException.class).printStackTrace();
			//CRMUtils.parseException(e).printStackTrace();
		} 
	}
	
	public static void execute0(){
        //String url = "http://127.0.0.1:8080/HessianServer/remoting/acrmapi";
		  String url = "http://192.168.1.201:8080/CRMServer/remoting/acrmapi";
        HessianCAHProxyFactory proxyFactory = new HessianCAHProxyFactory();
        proxyFactory.setConnectTimeout(3000);
        proxyFactory.setReadTimeout(3000); 
        proxyFactory.setHessian2Reply(false);
       // proxyFactory.setDebug(true);
        HessianCAHProxyFactory.setCookieSeterName("Set-Cookie");
        try {
        	AcrmApi api=proxyFactory.create(AcrmApi.class, url,new CRMCacheHandler(),new CRMAsynTaskExecuter<Object, Throwable>(),null,null);
        	//api.login("guxuede", "123",new CRMTaskListener());
        	//Thread.sleep(1000);
        	api.login("guxuede", "123");
        	System.out.println(GxdInputStream.getAccumulatTotalLength());
        	api.getContacts(new CRMDefaultTaskListener<List<String>,Throwable>(){
        		@Override
        		public void onExecuteSuccess(Method method, Object[] args,List<String> result) {
        			System.out.println("得到联系人列表："+result);
        		}
        		@Override
        		public void onExceptionOccured(Method method, Object[] args,Throwable exception) {
        			System.out.println("异常："+CRMUtils.getCaused(exception));
        		}
        	});
        	//System.out.println(api.getContacts());
        	//System.out.println(api.getContacts());
        	//System.out.println(api.uglilyCode("hessian"));
        	//System.out.println(api.getContacts());
        	//System.out.println(api.logout());
		} catch (Exception e) {
			e.printStackTrace();
			//System.out.println(((ServerThrowException)e.getCause()).getTypeId());
		} 
	}
	
	public static void execute1(){
        String url = "http://localhost:8080/HessianServer/remoting/acrmapi";
        HessianProxyFactory proxyFactory = new HessianHttpProxyFactory();
        proxyFactory.setConnectTimeout(3000);
        proxyFactory.setReadTimeout(3000); 
        proxyFactory.setHessian2Reply(false);
        proxyFactory.setDebug(true);
        try {
        	AcrmApi api=proxyFactory.create(AcrmApi.class, url);
        	System.out.println(api.login("guxuede", "123"));
        	//System.out.println(api.getContacts());
        	//System.out.println(api.uglilyCode("hessian"));
        	//System.out.println(api.getContacts());
        	//System.out.println(api.logout());
		} catch (Exception e) {
			e.printStackTrace();
			//System.out.println(((ServerThrowException)e.getCause()).getTypeId());
		} 
	}

	public static void parseAnnotations(Class c){
		Method[] methods = c.getDeclaredMethods();
		System.out.println(Arrays.toString(methods));
      Annotation[] annotations;
      for (Method method : methods) {
       annotations = method.getAnnotations();
       	for (Annotation annotation : annotations) {
          System.out.println(method.getName() + " : " + annotation.annotationType().getName());
       		}
      }
 }
}
